package net.child.chess.gui;

import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.content.Context;
import android.widget.AdapterView;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import net.child.chess.puzzles.*;

public class PuzzleSpinner extends Spinner implements OnItemSelectedListener, View.OnClickListener
{
	public PuzzleSpinner(Context context, BoardView boardView)
	{
		super(context);
		setOnItemSelectedListener(this);
		mBoardView = boardView;
		mPuzzles = new IPuzzle[MAX_PUZZLES];
		mPuzzles[MATE_BY_QUEEN_INDEX] = new MateByQueen();
		mPuzzles[MATE_IN_ONE_INDEX] = new MateInOne();
		
		String[] puzzles = new String[MAX_PUZZLES];
        for(int i=0; i<MAX_PUZZLES; ++i)
        {
        	puzzles[i] = mPuzzles[i].name();
        }
        LinearLayout.LayoutParams spinnerLayoutParam =
            new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT );
        setLayoutParams(spinnerLayoutParam);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context ,
                 android.R.layout.simple_spinner_item, puzzles); 
        setAdapter(adapter);
	}
	
	@Override
    public void onItemSelected(AdapterView<?> arg0, View arg1 ,int arg2, long arg3) 
	{
		onClick(null);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) 
    {
    } 
    
    //	This is handling the click on "Randomize" button.
    @Override
    public void onClick(View v)
    {
    	int index = getSelectedItemPosition();
    	mPuzzles[index].randomize(mBoardView.board());
        mBoardView.Reset();
    }
 

	private BoardView mBoardView;
	
	private static final int MATE_BY_QUEEN_INDEX    = 0;
	private static final int MATE_IN_ONE_INDEX      = 1;
	private static final int MAX_PUZZLES        	= 2;
	
	private IPuzzle mPuzzles[];
}
